*****************************************************
** Make panel dataset 
*****************************************************

use ..\processed_data\raw_data_all_years.dta, clear
** set up indicators for what types of deaths they were

** heroin and anything else
forvalues i = 1(1)20 {
	gen byte heroin`i' = (cond`i'=="T401")
}
egen any_heroin = rowmax(heroin*)
drop heroin*

** opioid and anything else
forvalues i = 1(1)20 {
	gen byte opioid`i' = (cond`i'=="T402") | (cond`i'=="T403") | (cond`i'=="T404")
}
egen any_opioid = rowmax(opioid*)
drop opioid*

** synthetic opioids and anything else
forvalues i = 1(1)20 {
	gen byte synthetic`i' = (cond`i'=="T404")
}
egen any_synthetic = rowmax(synthetic*)
drop synthetic*

** heroin or synthetic opioid
gen h_or_o = (any_heroin + any_opioid)>=1

** heroin without synthetic opioids
gen heroin_nosynths = any_heroin - any_synthetic
replace heroin_nosynths = 0 if heroin_nosynths<0
** opioids without synthetic opioids
gen opioid_nosynths = any_opioid - any_synthetic
replace opioid_nosynths = 0 if opioid_nosynths<0

drop cond*

collapse (sum) any_heroin any_opioid any_synthetic heroin_nosynths ///
opioid_nosynths h_or_o, ///
by(death_year death_month stateres)

rename death_year year
rename death_month month

sort stateres year month
merge m:1 stateres_fips using ..\processed_data\state_ids.dta
drop _merge
drop stateres_fips state_fullname
order fips
compress
sort fips year month

save ..\processed_data\panel_dataset.dta, replace

